package com.example;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by lizhiyong on 2015/9/24.
 */
public class SizeListParser {

    public static ArrayList<Size> parseList(File file) {
        ArrayList<Size> sizes = new ArrayList<>();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String tmp;
            while ((tmp = reader.readLine()) != null) {
                if (tmp.startsWith("//")) {
                    continue;
                }
                Size size = parseSize(tmp);
                if (size != null && !sizes.contains(size)) {
                    sizes.add(size);
                }
            }
        } catch (IOException e) {
            //            e.printStackTrace();
        }
        return sizes;
    }

    private static Size parseSize(String str) {
        try {
            Pattern pattern = Pattern.compile("\\d+x\\d+");
            Matcher matcher = pattern.matcher(str);
            StringBuilder sb = new StringBuilder();
            if (matcher.find()) {
                String sizeStr = matcher.group();
                System.out.println(sizeStr);
                String[] values = sizeStr.split("x");
                Size size = new Size();
                size.width = Integer.parseInt(values[0]);
                size.height = Integer.parseInt(values[1]);
                return size;
            }
        } catch (NumberFormatException e) {
            //            e.printStackTrace();
        }
        return null;
    }


}
